home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 7 / BBS in a Box - Macintosh - Volume VII (BBS in a Box) (January 1993).iso / Files / Tele / C / Comet2.1.3.cpt / emlib / h19key.c < prev    next >
Text File  |  1991-07-31  |  8KB  |  501 lines

  1. /*    Copyright Cornell University 1986.  All rights are reserved. */
  2.  
  3. #include <em.h>
  4.  
  5. #include <h19.h>
  6.  
  7.  
  8. #ifdef H19INPUT
  9.  
  10. /* following are the line 25 routines, which draw above the emulator screen where the controls sit */
  11.  
  12. Rect charrect;
  13.  
  14. h19key()
  15. {
  16.     short thechar;
  17.     EventRecord evt;
  18.  
  19.     GetNextEvent(keyDownMask|autoKeyMask, &evt);
  20.     if (evt.what == nullEvent) 
  21.         return(NONE);
  22.     else {
  23.         thechar = evt.message & 0xff;
  24.         if (evt.modifiers & cmdKey) {
  25.             if (thechar == '`' || thechar == '~')
  26.                 ;
  27.             else {
  28.                 /* command key down -- make it a control character */
  29.                 if (thechar == '1')
  30.                     thechar = F10;
  31.                 else
  32.                     thechar &= 0x1f;
  33.             }
  34.         }
  35.         if (thechar == BS)
  36.             if (!ba_bs)
  37.                 thechar = DELETE;
  38.         return(thechar);
  39.     }
  40.  
  41. }
  42.  
  43.  
  44. h19line(buf, length, echo)
  45. char *buf;
  46. unsigned length;
  47. char echo;
  48. {
  49.     short thechar;
  50.     EventRecord evt;
  51.     unsigned current;
  52.  
  53.     current = 0;
  54.     setcursor();
  55.     emprep();
  56.     while (TRUE) {
  57.         while (!GetNextEvent(keyDownMask|autoKeyMask, &evt))
  58.             ;
  59.         thechar = evt.message & 0xff;
  60.         if (evt.modifiers & cmdKey) {
  61.             /* command key down, interpret control chars */
  62.             switch (thechar) {
  63.                 case 'U': 
  64.                 case 'u': 
  65.                     /* clear entry */
  66.                     if (echo) {
  67.                         while (current) {
  68.                             em(BS);
  69.                             em(' ');
  70.                             em(BS);
  71.                             --current;
  72.                         }
  73.                         setcursor();
  74.                     }
  75.                     else
  76.                         current = 0;
  77.                     continue;
  78.                 case 'C':
  79.                 case 'c':
  80.                     /* completely punt this line */
  81.                     while (current) {
  82.                         em(BS);
  83.                         em(' ');
  84.                         em(BS);
  85.                         --current;
  86.                     }
  87.                     setcursor();
  88.                     buf[0] = '\0';
  89.                     emend();
  90.                     return(thechar & 0x1f);
  91.             }
  92.         }
  93.         switch (thechar) {
  94.             case CR:  
  95.                 buf[current] = '\0';
  96.                 emend();
  97.                 return(thechar);
  98.             case BS:
  99.                 if (current) {
  100.                     if (echo) {
  101.                         em(BS);
  102.                         em(' ');
  103.                         em(BS);
  104.                         setcursor();
  105.                     }
  106.                     --current;
  107.                     length++;
  108.                 }
  109.                 break;
  110.             default:
  111.                 ;
  112.         }
  113.         if (thechar < 32)
  114.             /* don't incorporate control chars */
  115.             continue;
  116.         if (!--length) {
  117.             /* no space in destination buffer */
  118.             beep();
  119.             continue;
  120.         }
  121.         if (echo) {
  122.             em(thechar);
  123.             setcursor();
  124.         }
  125.         buf[current++] = thechar;
  126.     }
  127.     emend();
  128. }
  129.  
  130.  
  131. get25line(buf, length, echo)
  132. char *buf;
  133. unsigned length;
  134. char echo;
  135. {
  136.     short thechar;
  137.     EventRecord evt;
  138.     unsigned current;
  139.     Point openloc;
  140.     struct fontsave ofont;
  141.     
  142.     savefont(&ofont);
  143.     
  144.     current = 0;
  145.  
  146.     GetPen(&openloc);
  147.     MoveTo(emdp->penloc25.h, emdp->penloc25.v);
  148.  
  149.     charrect.top = emdp->penloc25.v - 8;
  150.     charrect.left = emdp->penloc25.h - 1;
  151.     charrect.bottom = emdp->penloc25.v + 2;
  152.     charrect.right = emdp->penloc25.h + 5;
  153.     InvertRect(&charrect);
  154.  
  155.     while (TRUE) {
  156.         while (!GetNextEvent(keyDownMask|autoKeyMask, &evt))
  157.             ;
  158.         thechar = evt.message & 0xff;
  159.         if (evt.modifiers & cmdKey) {
  160.             /* command key down, interpret control chars */
  161.             switch (thechar) {
  162.                 case 'U': 
  163.                 case 'u': 
  164.                     /* clear entry */
  165.                     if (echo) {
  166.                         while (current) {
  167.                             do25char(BS);
  168.                             --current;
  169.                         }
  170.                     }
  171.                     else
  172.                         current = 0;
  173.                     continue;
  174.                 case 'C':
  175.                 case 'c':
  176.                     /* completely punt this line */
  177.                     while (current) {
  178.                         do25char(BS);
  179.                         --current;
  180.                     }
  181.                     buf[0] = '\0';
  182.                     emend();
  183.                     return(thechar & 0x1f);
  184.             }
  185.         }
  186.         switch (thechar) {
  187.             case CR:  
  188.                 buf[current] = '\0';
  189.                 EraseRect(&charrect);
  190.                 /* move cursor to beginning? */
  191.                 emend();
  192.                 return(thechar);
  193.             case BS:
  194.                 if (current) {
  195.                     if (echo) {
  196.                         do25char(BS);
  197.                     }
  198.                     --current;
  199.                     length++;
  200.                 }
  201.                 break;
  202.             default:
  203.                 ;
  204.         }
  205.         if (thechar < 32)
  206.             /* don't incorporate control chars */
  207.             continue;
  208.         if (!--length) {
  209.             /* no space in destination buffer */
  210.             beep();
  211.             continue;
  212.         }
  213.         if (echo) {
  214.             do25char(thechar);
  215.         }
  216.         buf[current++] = thechar;
  217.     }
  218.     /* reset the pen location */
  219.     thePort->pnLoc.h = openloc.h;
  220.     thePort->pnLoc.v = openloc.v;
  221.     
  222.     restorefont(&ofont);
  223. }
  224.  
  225.  
  226. do25char(thechar)
  227. char thechar;
  228. {
  229.     if (thechar == BS) {
  230.         EraseRect(&charrect);        /* blank cursor */
  231.         charrect.left -= FONTWIDTH;
  232.         charrect.right -= FONTWIDTH;
  233.         EraseRect(&charrect);        /* blank deleted char */
  234.         InvertRect(&charrect);    /* invert cursor */
  235.         Move(-FONTWIDTH, 0);
  236.     }
  237.     else {
  238.         EraseRect(&charrect);        /* blank cursor */
  239.         DrawChar(thechar);
  240.         charrect.left += FONTWIDTH;
  241.         charrect.right += FONTWIDTH;
  242.         InvertRect(&charrect);    /* invert cursor */
  243.     }
  244. }
  245.  
  246. #endif
  247.  
  248.  
  249.  
  250. /* the 25th line is above the emulator screen sharing space with
  251.     some buttons */
  252.  
  253. restore25()
  254. {
  255.     emdp->line25[0] = '\0';            /* so controls drawn on refresh */
  256.     if (emdp->emwindow == NULL)
  257.         return;
  258.  
  259.     emdp->controlschanged = TRUE;
  260.     InvalRect(&emdp->toprect);
  261. }
  262.  
  263.  
  264. /* clear the 25th line */
  265.  
  266. clr25()
  267. {
  268.     emdp->line25[0] = '\0';    /* so controls drawn on refresh */
  269.     if (emdp->emwindow == NULL)
  270.         return;
  271.     EraseRect(&emdp->toprect);
  272. }
  273.  
  274.  
  275. /* is the 25th line on? */
  276.  
  277. line25on(twp)
  278. struct winds * twp;
  279. {
  280.     if (twp->line25[0] != '\0')
  281.         return(TRUE);
  282.     else 
  283.         return(FALSE);
  284. }
  285.  
  286.  
  287. /* redraw the 25th line */
  288.  
  289. line25refresh()
  290. {
  291.     struct fontsave ofont;
  292.     
  293.     savefont(&ofont);
  294.     
  295.     MoveTo(6, -12);
  296.     DrawText(emdp->line25, 0, strlen(emdp->line25));
  297.  
  298.     restorefont(&ofont);
  299. }
  300.  
  301.  
  302.  
  303. /* Print an error message on the 25th line, setting a timer which will cause
  304.     it to be wiped out in 5 seconds. */
  305.  
  306. prerr25(strp)
  307.     char *strp; 
  308. {
  309.     if (!emwindow)
  310.         return;
  311.     
  312.     pr25(0, strp);
  313.     set25(5);
  314. }
  315.  
  316.  
  317. /* print an error from a context other than emdp, and don't erase it */
  318.  
  319. prerrint25(twp, strp)
  320. struct winds * twp;
  321. char *strp; 
  322. {
  323.     struct winds * oemdp;
  324.     
  325.     if (!emwindow)
  326.         return;
  327.     
  328.     oemdp = emdp;
  329.     if (twp != oemdp) {
  330.         getcontext(twp);
  331.     }
  332.     pr25(0, strp);
  333.     if (twp != oemdp) {
  334.         /* restore emdp context */
  335.         getcontext(oemdp);
  336.     }
  337. }
  338.  
  339.  
  340. /* print on line 25 at a given x */
  341.  
  342. prat25(s, offset)
  343. char *s;
  344. unsigned int offset; 
  345. {
  346.     register int count;
  347.     register char * linep;
  348.     int strlength;
  349.     struct fontsave ofont;
  350.     
  351.     GrafPtr oport;
  352.  
  353.     if (offset > LINE25LEN)
  354.         return(-1);
  355.  
  356.     /* get rid of nulls in the middle */
  357.     for (count = 0, linep = emdp->line25; count++ < offset; linep++) {
  358.         if (*linep == '\0')
  359.             *linep = ' ';
  360.     }
  361.     strlength = strlen(s);
  362.     if ((offset + strlength) > LINE25LEN) {
  363.         strlength = LINE25LEN - offset;
  364.     }
  365.     
  366.     /* copy the string */
  367.     strncpy(linep, s, (int) strlength);
  368.  
  369.     if (emwindow == NULL)
  370.         return;
  371.  
  372.     GetPort(&oport);
  373.     SetPort(emwindow);    
  374.         
  375.     savefont(&ofont);
  376.         
  377.     /* draw it */
  378.     MoveTo((short) (FONTWIDTH * offset) + 6, -12);
  379.     DrawText(s, 0, strlength);
  380.     
  381.     restorefont(&ofont);
  382.     SetPort(oport);    
  383.     
  384. }
  385.  
  386.  
  387. /* print a string starting at offset characters on the 25th line */
  388.  
  389. pr25(offset, strp)
  390. unsigned int offset;
  391. char *strp; 
  392. {
  393.     Rect eraserect;
  394.     short bitoffset;
  395.     int strlength;
  396.     Point openloc;
  397.     register int count;
  398.     register char * linep;
  399.     struct fontsave ofont;
  400.     GrafPtr oport;
  401.     
  402.     if (offset >= LINE25LEN)
  403.         return(-1);
  404.     
  405.     strlength = strlen(strp);
  406.     if ((offset + strlength) > LINE25LEN) {
  407.         strlength = LINE25LEN - offset - 1;
  408.     }
  409.  
  410.     /* get rid of everything in the middle */
  411.     for (count = 0, linep = emdp->line25; count++ < offset; linep++) {
  412.         /* if (*linep == '\0') */
  413.             *linep = ' ';
  414.     }
  415.     /* terminate it */
  416.     emdp->line25[offset + strlength] = '\0';
  417.  
  418.     /* copy the string */
  419.     strncpy(linep, strp, (int) strlength);
  420.  
  421.     if (emwindow == NULL)
  422.         return;
  423.  
  424.     GetPort(&oport);
  425.     SetPort(emwindow);    
  426.     
  427.     bitoffset = FONTWIDTH * offset + 6;
  428.  
  429.     savefont(&ofont);
  430.         
  431.     /* save old pen */
  432.     GetPen(&openloc);
  433.  
  434.     if (offset == 0) {
  435.         /* erase the whole top control area */
  436.         EraseRect(&emdp->toprect);
  437.     }
  438.     else {
  439.         /* erase the area that will be drawn on */
  440.         eraserect.top = -20;
  441.         eraserect.left = emdp->toprect.left + bitoffset;
  442.         eraserect.bottom = -5;
  443.         eraserect.right = emdp->toprect.right;
  444.         EraseRect(&eraserect);
  445.     }
  446.  
  447.     /* draw it */
  448.     MoveTo(bitoffset, -12);
  449.     DrawText(strp, 0, strlength);
  450.  
  451.     /* save the most recent penloc for get25line() */
  452.     emdp->penloc25.h = thePort->pnLoc.h;
  453.     emdp->penloc25.v = thePort->pnLoc.v;
  454.  
  455.     /* reset the pen location */
  456.     thePort->pnLoc.h = openloc.h;
  457.     thePort->pnLoc.v = openloc.v;
  458.  
  459.     restorefont(&ofont);
  460.     SetPort(oport);    
  461.     
  462.     /* guarantee it won't get cleared by a timer entry */
  463.     tm_clear(emdp->timer25);
  464. }
  465.  
  466.  
  467. /* timer routine to restore the buttons over line 25 */
  468.  
  469. reset25(windp)
  470. struct winds * windp;
  471. {
  472.     GrafPtr oport;
  473.     struct winds * owp;
  474.     
  475.     owp = emdp;
  476.     getcontext(windp);
  477.     
  478.     GetPort(&oport);
  479.     SetPort(windp->emwindow);
  480.     
  481.     if (windp->connopen)
  482.         restore25();
  483.  
  484.     tm_clear(windp->timer25);
  485.     
  486.     SetPort(oport);
  487.     
  488.     getcontext(owp);
  489. }
  490.  
  491.  
  492. /* set a timer to clear line 25 so we can use the buttons */
  493.  
  494. set25(secs)
  495. int secs;
  496. {
  497.     tm_set(secs, reset25, emdp, emdp->timer25);
  498. }
  499.  
  500.  
  501.